package vip.xiaonuo.biz.modular.watchtask.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.watchtask.entity.Task;
import vip.xiaonuo.biz.modular.watchtask.entity.TaskAssignment;
import vip.xiaonuo.biz.modular.watchtask.enums.TaskCurrentStatusEnum;
import vip.xiaonuo.biz.modular.watchtask.mapper.TaskAssignmentMapper;
import vip.xiaonuo.biz.modular.watchtask.mapper.TaskMapper;
import vip.xiaonuo.biz.modular.watchtask.param.TaskAssignmentPageParam;
import vip.xiaonuo.biz.modular.watchtask.result.TaskAssigmentUser;
import vip.xiaonuo.biz.modular.watchtask.service.TaskAssignmentService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 任务申请表(TaskAssignment)表服务实现类
 *
 * @author h
 * @since 2025-11-19 15:06:08
 */
@Service("taskAssignmentService")
public class TaskAssignmentServiceImpl extends ServiceImpl<TaskAssignmentMapper, TaskAssignment> implements TaskAssignmentService {
    @Resource
    private TaskMapper taskMapper;

    @Override
    public Page<TaskAssigmentUser> pageTaskAssigment(TaskAssignmentPageParam taskAssignmentPageParam) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t1.task_id", taskAssignmentPageParam.getTaskId());
        if(ObjectUtil.isAllNotEmpty(taskAssignmentPageParam.getSortField(), taskAssignmentPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(taskAssignmentPageParam.getSortOrder());
            queryWrapper.orderBy(true, taskAssignmentPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(taskAssignmentPageParam.getSortField()));
        } else {
            queryWrapper.orderByAsc("t1.create_time");
        }
        Page<TaskAssigmentUser> page = this.baseMapper.pageTaskAssigment(CommonPageRequest.defaultPage(), queryWrapper);
        for(TaskAssigmentUser taskAssigmentUser : page.getRecords()){
            //统计当前人员申请
            taskAssigmentUser.setApplyTaskNum(baseMapper.selectCount(new LambdaQueryWrapper<TaskAssignment>().eq(TaskAssignment::getAppletUserId, taskAssigmentUser.getAppletUserId())).intValue());
            List<Task> taskListTemp = taskMapper.selectList(new LambdaQueryWrapper<Task>().eq(Task::getReceiverAppletUserId, taskAssigmentUser.getAppletUserId()).eq(Task::getCurrentStatus, TaskCurrentStatusEnum.FINISHED.getValue()));
            //统计当前人员完成任务数量
            taskAssigmentUser.setCompleteTaskNum(taskListTemp.size());
            //统计当前人员完成金额
            taskAssigmentUser.setCompleteRewardNum(taskListTemp.stream().mapToDouble(Task::getRewards).sum()/100);
        }
        return page;
    }
}
